<?php

class Page extends Eloquent {
    protected $guarded = ['id'];
    protected $fillable = ['titulo', 'slug','texto','estado','position'];
    public $timestamps = false;
    
    public function images(){
        return $this->belongsToMany('Imageapa', 'imageapa_page')->withPivot('position');
    }
    
    public function imagesOrdered(){
        return $this->images()->orderBy('position');
    }
    
    public static function validate($input) {
        $rules = [
            'titulo' => 'required|max:50',
            'texto' => 'required',
        ];
        return Validator::make($input, $rules);
    }
    
    public function scopeActive($query) {
        return $query->where('estado', 1);
    }
    
    public static function scopeGetFirst($query){
        return $query->active()->orderBy('position')->take(1)->first();
    }
}